﻿using Cysharp.Threading.Tasks;
using Framework.Manager;
using UnityEngine;

namespace Framework.Task
{
    public class Task_LoadScene : Task
    {
        private readonly string scenePath;
        private readonly bool allowSceneActivation;
        
        public Task_LoadScene(int factor, string scenePath, bool allowSceneActivation) : base(factor, "load scene")
        {
            this.scenePath = scenePath;
            this.allowSceneActivation = allowSceneActivation;
        }
        
        protected override async UniTask OnStart()
        {
            await ResourceManager.Instance.LoadSceneAsync(scenePath, (ao) =>
            {
                Shared.Shared.loadSceneAo = ao;
                ao.allowSceneActivation = allowSceneActivation;
            }, OnProgress, OnFinish);
        }

        private async void OnFinish(AsyncOperation ao)
        {
            OnProgress(1);
            await OnFinished();
        }
    }
}